<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>absolute和负margin实现</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        .container {
            width: 100%;
            height: 100%;
            position: relative;
        }

        .center {
            /* 首先用绝对定位移动到中间（左上角） */
            position: absolute;
            left: 50%;
            top: 50%;
            /* 再移动自身的一半 */
            margin-left: -100px;
            margin-top: -100px;
            width: 200px;
            height: 200px;
            background-color: lightpink;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="center"></div>
    </div>
</body>

</html>